/*     */ package com.cr.system.service.impl;
/*     */ 
/*     */ import com.cr.common.core.domain.Ztree;
/*     */ import com.cr.common.core.domain.entity.SysDictData;
/*     */ import com.cr.common.core.domain.entity.SysDictType;
/*     */ import com.cr.common.core.text.Convert;
/*     */ import com.cr.common.exception.ServiceException;
/*     */ import com.cr.common.utils.DictUtils;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.system.mapper.SysDictDataMapper;
/*     */ import com.cr.system.mapper.SysDictTypeMapper;
/*     */ import com.cr.system.service.ISysDictTypeService;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.annotation.PostConstruct;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Service;
/*     */ import org.springframework.transaction.annotation.Transactional;
/*     */ 
/*     */ @Service
/*     */ public class SysDictTypeServiceImpl implements ISysDictTypeService {
/*     */   @Autowired
/*     */   private SysDictTypeMapper dictTypeMapper;
/*     */   
/*     */   @Autowired
/*     */   private SysDictDataMapper dictDataMapper;
/*     */   
/*     */   @PostConstruct
/*     */   public void init() {
/*  41 */     loadingDictCache();
/*     */   }
/*     */   
/*     */   public List<SysDictType> selectDictTypeList(SysDictType dictType) {
/*  53 */     return this.dictTypeMapper.selectDictTypeList(dictType);
/*     */   }
/*     */   
/*     */   public List<SysDictType> selectDictTypeAll() {
/*  64 */     return this.dictTypeMapper.selectDictTypeAll();
/*     */   }
/*     */   
/*     */   public List<SysDictData> selectDictDataByType(String dictType) {
/*  76 */     List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
/*  77 */     if (MyStringUtils.isNotEmpty(dictDatas))
/*  79 */       return dictDatas; 
/*  81 */     dictDatas = this.dictDataMapper.selectDictDataByType(dictType);
/*  82 */     if (MyStringUtils.isNotEmpty(dictDatas)) {
/*  84 */       DictUtils.setDictCache(dictType, dictDatas);
/*  85 */       return dictDatas;
/*     */     } 
/*  87 */     return null;
/*     */   }
/*     */   
/*     */   public SysDictType selectDictTypeById(Long dictId) {
/*  99 */     return this.dictTypeMapper.selectDictTypeById(dictId);
/*     */   }
/*     */   
/*     */   public SysDictType selectDictTypeByType(String dictType) {
/* 111 */     return this.dictTypeMapper.selectDictTypeByType(dictType);
/*     */   }
/*     */   
/*     */   public void deleteDictTypeByIds(String ids) {
/* 123 */     Long[] dictIds = Convert.toLongArray(ids);
/* 124 */     for (Long dictId : dictIds) {
/* 126 */       SysDictType dictType = selectDictTypeById(dictId);
/* 127 */       if (this.dictDataMapper.countDictDataByType(dictType.getDictType()) > 0)
/* 129 */         throw new ServiceException(String.format("%1$s已分配,不能删除", new Object[] { dictType.getDictName() })); 
/* 131 */       this.dictTypeMapper.deleteDictTypeById(dictId);
/* 132 */       DictUtils.removeDictCache(dictType.getDictType());
/*     */     } 
/*     */   }
/*     */   
/*     */   public void loadingDictCache() {
/* 142 */     List<SysDictType> dictTypeList = this.dictTypeMapper.selectDictTypeAll();
/* 143 */     for (SysDictType dict : dictTypeList) {
/* 145 */       List<SysDictData> dictDatas = this.dictDataMapper.selectDictDataByType(dict.getDictType());
/* 146 */       DictUtils.setDictCache(dict.getDictType(), dictDatas);
/*     */     } 
/*     */   }
/*     */   
/*     */   public void clearDictCache() {
/* 156 */     DictUtils.clearDictCache();
/*     */   }
/*     */   
/*     */   public void resetDictCache() {
/* 165 */     clearDictCache();
/* 166 */     loadingDictCache();
/*     */   }
/*     */   
/*     */   public int insertDictType(SysDictType dict) {
/* 178 */     int row = this.dictTypeMapper.insertDictType(dict);
/* 179 */     if (row > 0)
/* 181 */       DictUtils.setDictCache(dict.getDictType(), null); 
/* 183 */     return row;
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public int updateDictType(SysDictType dict) {
/* 196 */     SysDictType oldDict = this.dictTypeMapper.selectDictTypeById(dict.getDictId());
/* 197 */     this.dictDataMapper.updateDictDataType(oldDict.getDictType(), dict.getDictType());
/* 198 */     int row = this.dictTypeMapper.updateDictType(dict);
/* 199 */     if (row > 0) {
/* 201 */       List<SysDictData> dictDatas = this.dictDataMapper.selectDictDataByType(dict.getDictType());
/* 202 */       DictUtils.setDictCache(dict.getDictType(), dictDatas);
/*     */     } 
/* 204 */     return row;
/*     */   }
/*     */   
/*     */   public String checkDictTypeUnique(SysDictType dict) {
/* 216 */     Long dictId = Long.valueOf(MyStringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId().longValue());
/* 217 */     SysDictType dictType = this.dictTypeMapper.checkDictTypeUnique(dict.getDictType());
/* 218 */     if (MyStringUtils.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue())
/* 220 */       return "1"; 
/* 222 */     return "0";
/*     */   }
/*     */   
/*     */   public List<Ztree> selectDictTree(SysDictType dictType) {
/* 234 */     List<Ztree> ztrees = new ArrayList<>();
/* 235 */     List<SysDictType> dictList = this.dictTypeMapper.selectDictTypeList(dictType);
/* 236 */     for (SysDictType dict : dictList) {
/* 238 */       if ("0".equals(dict.getStatus())) {
/* 240 */         Ztree ztree = new Ztree();
/* 241 */         ztree.setId(dict.getDictId());
/* 242 */         ztree.setName(transDictName(dict));
/* 243 */         ztree.setTitle(dict.getDictType());
/* 244 */         ztrees.add(ztree);
/*     */       } 
/*     */     } 
/* 247 */     return ztrees;
/*     */   }
/*     */   
/*     */   public String transDictName(SysDictType dictType) {
/* 252 */     StringBuffer sb = new StringBuffer();
/* 253 */     sb.append("(" + dictType.getDictName() + ")");
/* 254 */     sb.append("&nbsp;&nbsp;&nbsp;" + dictType.getDictType());
/* 255 */     return sb.toString();
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-system-4.7.0.jar!\com\cr\system\service\impl\SysDictTypeServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */